<template>
  <div>
    <h3>注册页面</h3>
    <el-form :model="ruleUser" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm" style="width:500px;margin:0 auto">
        <el-form-item label="用户名" prop="username">
            <el-input  v-model="ruleUser.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
            <el-input type="password" v-model="ruleUser.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="password2">
            <el-input type="password" v-model="ruleUser.password2" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="mobile">
            <el-input  v-model="ruleUser.mobile" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="验证码" prop="imagecode">
            <el-input  v-model="ruleUser.imagecode" autocomplete="off"></el-input>
            <div>
              <img :src="imageUrl" alt="图形验证码"  @click="getImage">
            </div>
        </el-form-item>
        <el-form-item  prop="agree">
          <el-checkbox v-model="ruleUser.agree">请勾选用户协议</el-checkbox>
        </el-form-item>
        
        <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">注册</el-button>
            <!-- <el-button @click="resetForm('ruleForm')">重置</el-button> -->
        </el-form-item>
</el-form>
  </div>
</template>

<script>
import {v4 as uuid4} from 'uuid'
import axios from 'axios'
  export default {
    data() {
      var validateUsername = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入用户名'));
        } 
        const rulesPass=/^[a-zA-Z][A-Za-z0-9]{5,16}$/
        if (rulesPass.test(value)){
          axios.get('http://127.0.0.1:8000/users/checkname/'+value+'/')
          .then(resp=>{
            console.log(resp);
            if(resp.data.code ==200){
              return callback()
            }
            else{
              return callback(resp.data.msg)
            }
          })
          .catch(err=>{
            console.log(err);
          })
          
          
        }else{
          callback(new Error('用户名格式不正确'));
        }
      };
      
      var validatePassword = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } 
        const rulesPass=/^[a-zA-Z][A-Za-z0-9]{5,16}$/
        if (rulesPass.test(value)){
          return callback()
        }else{
          callback(new Error('密码格式不正确'));
        }
      };
      var validatePassword2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.ruleUser.password2) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
      var validateMobile = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入手机号'));
        } 
        const rulesPass=/^1[3-9][0-9]{9}$/
        if (rulesPass.test(value)){
          axios.get('http://127.0.0.1:8000/users/checkmobile/'+value+'/')
          .then(resp=>{
            console.log(resp);
            if(resp.data.code ==200){
              return callback()
            }
            else{
              return callback(resp.data.msg)
            }
          })
          .catch(err=>{
            console.log(err);
          })
        }else{
          callback(new Error('手机号格式不正确'));
        }
      };


      var validateImagecode = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入验证码'));
        } 
        const rulesPass=/^[0-9A-Za-z]{4}$/
        if (rulesPass.test(value)){
          axios.get('http://127.0.0.1:8000/users/checkimage/'+this.imageID+'/',{code:value})
          .then(resp=>{
            if(resp.data.code ==200){
              return callback()
            }
            else{
              return callback(resp.data.msg)
            }
          })
          .catch(err=>{
            console.log(err);
          })
        }else{
          callback(new Error('验证码格式不正确'));
        }
      };
      return {
        imageUrl:'',
        imageID:'',
        ruleUser: {
          username:'',
          password: '',
          password2:'',
          checkPass: '',
          mobile: '',
          imagecode:'',
          agree:false,
        },
        rules: {
          username: [
            { validator: validateUsername, trigger: 'blur' }
          ],
          password: [
            { validator: validatePassword, trigger: 'blur' }
          ],
          password2: [
            { validator: validatePassword2, trigger: 'blur' }
          ],
          mobile: [
            { validator: validateMobile, trigger: 'blur' }
          ],
          imagecode: [
            { validator: validateImagecode, trigger: 'blur' }
          ],
         
        }
      };
    },
    mounted(){
      this.getImage()
    },
    methods: {
      getImage(){
        this.imageID=uuid4()
        this.imageUrl='http://127.0.0.1:8000/users/checkimage/'+this.imageID+'/'
      },
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            axios.post('http://127.0.0.1:8000/users/reg/',this.ruleUser)
            .then(resp=>{
              console.log(resp);
              if(resp.data.code ==200){
                alert('注册成功')
                this.$router.push('/login')
              }else{
                alert(resp.data.msg)
              }
            })
            .catch(err=>{
              console.log(err);
            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
</script>

<style>

</style>